package com.example.util;

import cn.hutool.core.util.StrUtil;

import java.security.MessageDigest;

/**
 * @ClassName Md5Util
 * @Description TODO
 * @Author kun
 * @Date 2024-10-23 12:35
 * @Version 1.0
 */
public class Md5Util {
    public static String md5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes=str.getBytes("UTF-16LE");
            md.update(bytes);
            byte[] result = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < result.length; i++) {
                int val = result[i] & 0xff;
                String hex=Integer.toHexString(val);
                hex= StrUtil.padPre(hex, 2, '0');
                sb.append(hex);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
